/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ambientes.java
 *
 * Created on Nov 20, 2009, 11:36:45 PM
 */

package simulador;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

/**
 *
 * @author rangel
 */
public class Ambientes extends javax.swing.JDialog {
    /** Creates new form Ambientes */
    public Ambientes(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
		try {
			carregaAmbientes(new File("ambientes"));
		} catch (FileNotFoundException ex) {
			Logger.getLogger(Ambientes.class.getName()).log(Level.SEVERE, null, ex);
		}
		/*
		 * Ajusta LookAndFeel
		 */
		try {
			javax.swing.UIManager.LookAndFeelInfo[] looks = javax.swing.UIManager.getInstalledLookAndFeels();
			javax.swing.UIManager.setLookAndFeel(looks[looks.length-1].getClassName());  
			javax.swing.SwingUtilities.updateComponentTreeUI(this);  
		}catch(Exception e) {  
			e.printStackTrace();  
		}
		/*
		 * Fecha dialog ao receber ESC
		 */
		javax.swing.KeyStroke stroke = javax.swing.KeyStroke.getKeyStroke("ESCAPE");
		javax.swing.Action actionListener = new AbstractAction() { 
			public void actionPerformed(ActionEvent actionEvent) {
				setVisible(false);
			} 
		} ;
		javax.swing.InputMap inputMap = rootPane.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW);
		inputMap.put(stroke, "ESCAPE");
		rootPane.getActionMap().put("ESCAPE", actionListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonGroup1 = new javax.swing.ButtonGroup();
    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    buttonAplicar = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Ambientes");
    setResizable(false);

    jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ambientes"));

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Nome", "Custo", "Cor"
      }
    ) {
      boolean[] canEdit = new boolean [] {
        false, false, false
      };

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
      }
    });
    jScrollPane1.setViewportView(jTable1);

    buttonAplicar.setText("Aplicar");
    buttonAplicar.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonAplicarActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
          .addComponent(buttonAplicar, javax.swing.GroupLayout.Alignment.TRAILING))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
        .addComponent(buttonAplicar)
        .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-578)/2, (screenSize.height-369)/2, 578, 369);
  }// </editor-fold>//GEN-END:initComponents

		private void buttonAplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAplicarActionPerformed
			// TODO add your handling code here:
			if(jTable1.getSelectedRow()<0)
				JOptionPane.showMessageDialog(this, "Escolha o tipo ambiente que deseja criar!", "Simulador", JOptionPane.ERROR_MESSAGE);
			else
				setVisible(false);

		}//GEN-LAST:event_buttonAplicarActionPerformed


		private void carregaAmbientes(File file) throws FileNotFoundException{
			Scanner sc = new Scanner(file);
			Object linha[];
			DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
			while(sc.hasNext()){
				linha = new Object[3];
				linha[0] = sc.next();
				linha[1] = sc.next();
				linha[2] = sc.next();
				model.addRow(linha);
			}
		}

		public float getCost(){
			int row = jTable1.getSelectedRow();
			if(row<0)
				return -1;
			String c = jTable1.getValueAt(row, 1).toString();
			return Float.parseFloat(c);
		}

		public Color getColor(){
			int row = jTable1.getSelectedRow();
			if(row<0)
				return null;
			String c = jTable1.getValueAt(row, 2).toString();
			return Color.decode(c);
		}

		public String getAmbiente(){
			int row = jTable1.getSelectedRow();
			if(row<0)
				return null;
			return jTable1.getValueAt(row, 0).toString();
		}
		
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Ambientes dialog = new Ambientes(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton buttonAplicar;
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable jTable1;
  // End of variables declaration//GEN-END:variables

}
